package org.pizza.autoconfigure;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 高巍
 * @since  2020-05-27 3:25 下午
 */
@Configuration
@Slf4j
public class XXLJobAutoConfiguration {

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${spring.application.name}")
    private String appName;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Value("${spring.profiles.active}")
    private String profile;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor(InetUtils inetUtils) {
        String ip = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        xxlJobSpringExecutor.setAppname(appName + "-" + profile);
        log.info("xxl-job XxlJobSpringExecutor init -> appName:{} ip:{} port:{}", appName + "-" + profile, ip, port);
        return xxlJobSpringExecutor;
    }

}
